package com.answer.service.impl;

import cn.hutool.json.JSONUtil;
import com.answer.mapper.QuestionMapper;
import com.answer.mapper.UserMapper;
import com.answer.model.dto.question.*;
import com.answer.model.entity.Question;
import com.answer.model.entity.User;
import com.answer.model.reslut.ResultPage;
import com.answer.model.vo.QuestionVo;
import com.answer.model.vo.UserVo;
import com.answer.service.QuestionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author haoshuai
* @description 针对表【question(题目)】的数据库操作Service实现
* @createDate 2024-06-29 15:23:08
*/
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
    implements QuestionService{

    @Resource
    private UserMapper userMapper;

    @Override
    public Boolean addQuestion(QuestionAddDTO questionAddDTO) {

        Question question = new Question();
        BeanUtils.copyProperties(questionAddDTO, question);
        List<QuestionContentDTO> questionContentDTO = questionAddDTO.getQuestionContentDTO();
        question.setQuestionContent(JSONUtil.toJsonStr(questionContentDTO));
        int res = baseMapper.insert(question);



        return res==1;
    }

    @Override
    public ResultPage<QuestionVo> queryAll(QuestionQueryDTO questionQueryDTO) {
        Page page = new Page(questionQueryDTO.getCurrent(), questionQueryDTO.getSize());
        baseMapper.selectPage(page, getQueryWrapper(questionQueryDTO));
        List<Question> questions = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long total = page.getTotal();
        List<QuestionVo> appVoList = new ArrayList<>();
        for (Question question : questions) {
            User user = userMapper.selectById(question.getUserId());
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            QuestionVo appVo = new QuestionVo();
            BeanUtils.copyProperties(question, appVo);
            List<QuestionContentDTO> questionContentDTOS = JSONUtil.toList(question.getQuestionContent(), QuestionContentDTO.class);
            appVo.setQuestionContentDTO(questionContentDTOS);
            appVo.setUserVo(userVo);
            appVoList.add(appVo);
        }

        ResultPage resultPage = new ResultPage();
        resultPage.setRecords(appVoList);
        resultPage.setCurrent(current);
        resultPage.setPages(pages);
        resultPage.setTotal(total);
        return resultPage;
    }

    @Override
    public QuestionVo queryOneById(Long id) {

        Question question = baseMapper.selectById(id);
        QuestionVo appVo = new QuestionVo();
        BeanUtils.copyProperties(question, appVo);

        User user = userMapper.selectById(question.getUserId());
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        List<QuestionContentDTO> questionContentDTOS = JSONUtil.toList(question.getQuestionContent(), QuestionContentDTO.class);
        appVo.setQuestionContentDTO(questionContentDTOS);
        appVo.setUserVo(userVo);
        return appVo;
    }

    @Override
    public Boolean updateQuestion(QuestionUpdateDTO questionUpdateDTO) {
        Question question = new Question();
        BeanUtils.copyProperties(questionUpdateDTO, question);
        List<QuestionContentDTO> questionContentDTO = questionUpdateDTO.getQuestionContentDTO();
        question.setQuestionContent(JSONUtil.toJsonStr(questionContentDTO));
        int i = baseMapper.updateById(question);
        return i == 1;
    }

    @Override
    public Boolean editQuestion(QuestionEditDTO questionEditDTO) {
        Question question = new Question();
        BeanUtils.copyProperties(questionEditDTO, question);
        List<QuestionContentDTO> questionContentDTO = questionEditDTO.getQuestionContent();
        question.setQuestionContent(JSONUtil.toJsonStr(questionContentDTO));
        int i = baseMapper.updateById(question);
        return i == 1;
    }

    @Override
    public List<HashMap<String, String>> getQuestinIdAndNameMap() {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper();
//        wrapper.select(Question::getId, Question::getTopic);
        List<Question> questionList = baseMapper.selectList(wrapper);

        List<HashMap<String, String>> res = new ArrayList<>();
        if (questionList != null && !questionList.isEmpty()) {
            for (Question question : questionList) {
                HashMap<String, String> hashMap = new HashMap<>();
                Long id = question.getId();
//                String topic = question.getTopic();
                hashMap.put("value", id.toString());
//                hashMap.put("label", topic);
                res.add(hashMap);
            }
        } else {

        }
        return res;
    }


    private LambdaQueryWrapper<Question> getQueryWrapper(QuestionQueryDTO questionQueryDTO) {
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(questionQueryDTO.getAppId()), Question::getAppId, questionQueryDTO.getAppId());
        return queryWrapper;
    }

}




